# -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.

from odoo import api, fields, models, _, tools


SQL_SELECT = '''
    SELECT pap.id, 
        pap.id as paper_id,
        pap.create_uid, pap.write_uid,
        pap.create_date, pap.write_date
    FROM fp_fapiao_paper pap
    WHERE pap.type in ('in_invoice', 'in_refund')
'''


class FapiaoBill(models.Model):
    _name = "fp.bill"
    _description = "Fapiao Bill"
    _auto = False
    _order = 'date_fapiao desc, id desc'

    def init(self):
        tools.drop_view_if_exists(self.env.cr, self._table)
        query = "CREATE OR REPLACE VIEW %s AS (%s)" % (
            self._table, SQL_SELECT)
        self.env.cr.execute(query % ())

    paper_id = fields.Many2one(
        'fp.fapiao.paper', required=False, ondelete='cascade', index=True,
        string='Related Fapiao Paper', help='')

    name = fields.Char('Bill Name', related='paper_id.name')
    state = fields.Selection(related='paper_id.state')
    type = fields.Selection(related='paper_id.type')
    attachment_id = fields.Many2one(related='paper_id.attachment_id')

    company_id = fields.Many2one(related='paper_id.company_id')
    user_id = fields.Many2one(related='paper_id.user_id')

    invoice_type = fields.Selection(related='paper_id.invoice_type')
    date_fapiao = fields.Date(related='paper_id.date_fapiao')
    invoice_code = fields.Char(related='paper_id.invoice_code')
    invoice_number = fields.Char(related='paper_id.invoice_number')
    check_code = fields.Char(related='paper_id.check_code')
    taxmachine_code = fields.Char(related='paper_id.taxmachine_code')
    note = fields.Text(related='paper_id.note')
    payee_name = fields.Char(related='paper_id.payee_name')
    checker_name = fields.Char(related='paper_id.checker_name')
    drawer_name = fields.Char(related='paper_id.drawer_name')

    amount_untaxed = fields.Float(related='paper_id.amount_untaxed')
    amount_tax = fields.Float(related='paper_id.amount_tax')
    amount_total = fields.Float(related='paper_id.amount_total')
    cn_amount_total = fields.Char(related='paper_id.cn_amount_total')

    buyer_id = fields.Many2one(
        related='paper_id.buyer_id',
        help="票面的购买方, 进项票应该为我方公司.")

    buyer_name = fields.Char(related='buyer_id.fapiao_name')
    buyer_tin = fields.Char(related='buyer_id.tin')

    company_partner_id = fields.Many2one(
        'fp.partner', string='Company Partner',
        compute='_compute_company_partner',
        help="公司开票信息, 用于比对票面的购买方")

    partner_id = fields.Many2one(
        'fp.partner', string='Partner', related='paper_id.saler_id',
        help="票面的销售方,即供应商信息")

    company_name = fields.Char(
        'Company Name', compute='_compute_company_partner')
    company_tin = fields.Char(
        'Company TIN', compute='_compute_company_partner')
    company_address = fields.Char(
        'Company Address', compute='_compute_company_partner')
    company_phone = fields.Char(
        'Company Phone', compute='_compute_company_partner')
    company_bank_name = fields.Char(
        'Company Bank Name', compute='_compute_company_partner')
    company_acc_number = fields.Char(
        string='Company Bank Account', compute='_compute_company_partner')

    company_address_phone = fields.Char(
        string='Company Address Phone', compute='_compute_company_partner')
    company_bank_account = fields.Char(
        string='Company Bank And Account', compute='_compute_company_partner')

    @api.depends('company_id')
    def _compute_company_partner(self):
        for one in self:
            comp_partner = one.company_id._get_fp_partner()
            one.company_partner_id = comp_partner
            one.company_name = comp_partner.fapiao_name
            one.company_tin = comp_partner.tin
            one.company_address = comp_partner.address
            one.company_phone = comp_partner.phone
            one.company_bank_name = comp_partner.bank_name
            one.company_acc_number = comp_partner.acc_number
            one.company_address_phone = comp_partner.address_phone
            one.company_bank_account = comp_partner.bank_account

    partner_name = fields.Char(
        string='Partner Name', related='partner_id.fapiao_name')
    partner_tin = fields.Char(
        string='Partner TIN', related='partner_id.tin')
    partner_address = fields.Char(
        string='Partner Address', related='partner_id.address')
    partner_phone = fields.Char(
        string='Partner Phone', related='partner_id.phone')
    partner_bank_name = fields.Char(
        string='Partner Bank Name', related='partner_id.bank_name')
    partner_acc_number = fields.Char(
        string='Partner Bank Account', related='partner_id.acc_number')

    partner_address_phone = fields.Char(
        string='Partner Address Phone', related='partner_id.address_phone')
    partner_bank_account = fields.Char(
        string='Partner Bank And Account', related='partner_id.bank_account')

    line_ids = fields.One2many(
        'fp.bill.line', 'fapiao_id', string='Fapiao Lines',
        states={'cancel': [('readonly', True)], 'done': [('readonly', True)]},
        copy=True, auto_join=True)

    #
    # Actions
    #

    # def action_confirm(self):
    #     self2 = self.search([('id', 'in', self.ids), ('state', '=', 'draft')])
    #     for one in self2:
    #         one.write({'state': 'confirmed'})

    # def action_cancel(self):
    #     self2 = self.search([('id', 'in', self.ids),
    #                          ('state', 'in', ['draft', 'confirmed'])])

    #     for one in self2:
    #         one.write({'state': 'cancel'})

    # def action_draft(self):
    #     self2 = self.search([('id', 'in', self.ids), ('state', '=', 'cancel')])
    #     for one in self2:
    #         one.write({'state': 'draft'})
